package com.wgchao.identityrightsmanagement.application.controller;

import com.wgchao.identityrightsmanagement.application.service.MenuApplicationSerive;
import com.wgchao.identityrightsmanagement.application.service.ProjectApplicationService;
import com.wgchao.identityrightsmanagement.domain.resource.model.*;
import com.wgchao.identityrightsmanagement.infrastructure.utils.ResponseData;
import com.wgchao.identityrightsmanagement.query.ResourcesQuery;
import com.wgchao.identityrightsmanagement.query.vo.MenuDetailVo;
import com.wgchao.identityrightsmanagement.query.vo.ProjectVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: ResourceController
 * @author: wgchao
 * @createTime: 2020/6/18 10:13 AM
 * @Description: TODO
 */
@RestController
@RequestMapping("/sntk/sys/resource")
@Api(value = "ResourceController", description = "资源管理")
public class ResourceController {
    @Autowired
    private ProjectApplicationService projectApplicationService;
    @Autowired
    private MenuApplicationSerive menuApplicationSerive;
    @Autowired
    private ResourcesQuery resourcesQuery;


    /**************************项目资源***************************/

    @ApiOperation(value = "添加项目资源")
    @PostMapping("/project")
    public ResponseData addProject(@ApiParam(name = "creator", value = "项目信息", required = true) @RequestBody ProjectResourcesCreator creator){
        projectApplicationService.addProjectResources(creator, "admin");
        return ResponseData.success();
    }


    @ApiOperation(value = "修改项目资源信息")
    @PutMapping("/project/{proMark}")
    public ResponseData modifyProjectInfo(@ApiParam(name = "proMark", value = "项目标识", required = true) @PathVariable String proMark,
                                          @ApiParam(name = "updator", value = "项目信息", required = true) @RequestBody ProjectResourcesUpdator updator){
        projectApplicationService.modifyProjectInfo(proMark, updator, "admin");
        return ResponseData.success();
    }

    @ApiOperation(value = "删除项目资源")
    @DeleteMapping("/project/{proMark}")
    public ResponseData removeProject(@ApiParam(name = "proMark", value = "项目标识", required = true) @PathVariable String proMark){
        projectApplicationService.removeProject(proMark, "admin");
        return ResponseData.success();
    }
    @ApiOperation(value = "禁用项目")
    @PutMapping("/project/disabled/{proMark}")
    public ResponseData disabled(@ApiParam(name = "proMark", value = "项目标识", required = true) @PathVariable String proMark){
        projectApplicationService.disable(proMark, "admin");
        return ResponseData.success();
    }

    @ApiOperation(value = "启用项目")
    @PutMapping("/project/regain/{proMark}")
    public ResponseData regain(@ApiParam(name = "proMark", value = "项目标识", required = true) @PathVariable String proMark){
        projectApplicationService.regain(proMark, "admin");
        return ResponseData.success();
    }


    @ApiOperation(value = "查询项目基本信息")
    @GetMapping("/project/{proMark}")
    public ResponseData<ProjectVo> findByMark(@ApiParam(name = "proMark", value = "项目标识", required = true) @PathVariable String proMark){
        ProjectVo projectVo = resourcesQuery.findByMark(proMark);
        return ResponseData.success().setData(projectVo);
    }

    @ApiOperation(value = "查询全部项目基本信息")
    @GetMapping("/all/project")
    public ResponseData<List<ProjectVo>> findAllProject(){
        List<ProjectVo> projectVos = resourcesQuery.findAllProject();
        return ResponseData.success().setData(projectVos);
    }


    /**************************菜单资源***************************/

    @ApiOperation(value = "添加菜单资源")
    @PostMapping("/menu/{proMark}")
    public ResponseData addMenuToProject(@ApiParam(name = "proMark", value = "项目标识", required = true) @PathVariable String proMark,
                                         @ApiParam(name = "creator", value = "菜单信息", required = true) @RequestBody MenuResourceCreator creator){
        menuApplicationSerive.addMenuToProject(proMark, creator, "admin");
        return ResponseData.success();
    }

    @ApiOperation(value = "修改菜单资源信息")
    @PutMapping("/menu/{menuMark}")
    public ResponseData modifyMenuInfo(@ApiParam(name = "menuMark", value = "菜单标识", required = true) @PathVariable String menuMark,
                                         @ApiParam(name = "updator", value = "菜单信息", required = true) @RequestBody MenuResourcesUpdator updator){
        menuApplicationSerive.modifyMenuInfo(menuMark, updator, "admin");
        return ResponseData.success();
    }

    @ApiOperation(value = "删除菜单资源")
    @DeleteMapping("/menu/{menuMark}")
    public ResponseData removeMenu(@ApiParam(name = "menuMark", value = "菜单标识", required = true) @PathVariable String menuMark){
        menuApplicationSerive.removeMenu(menuMark, "admin");
        return ResponseData.success();
    }

    @ApiOperation(value = "为菜单添加功能资源")
    @PostMapping("/menu/func/{menuMark}")
    public ResponseData addFuncToMenu(@ApiParam(name = "menuMark", value = "菜单标识", required = true) @PathVariable String menuMark,
                                      @ApiParam(name = "creator", value = "菜单功能信息", required = true) @RequestBody FuncResourcesCreator creator){
        menuApplicationSerive.addFuncToMenu(menuMark, creator, "admin");
        return ResponseData.success();
    }

    @ApiOperation(value = "删除功能资源")
    @DeleteMapping("/menu/func/{menuMark}/{funcMark}")
    public ResponseData removeFunc(@ApiParam(name = "menuMark", value = "菜单标识", required = true) @PathVariable String menuMark,
                                   @ApiParam(name = "funcMark", value = "功能标识", required = true) @PathVariable String funcMark){
        menuApplicationSerive.removeFunc(menuMark, funcMark, "admin");
        return ResponseData.success();
    }

    @ApiOperation(value = "查询项目下菜单信息")
    @GetMapping("/{proMark}/menu/detail")
    public ResponseData<List<MenuDetailVo>> findMenuDetailsOfPro(@ApiParam(name = "proMark", value = "项目标识", required = true) @PathVariable String proMark){
        List<MenuDetailVo> menuDetailVos = resourcesQuery.findMenuDetailsOfPro(proMark);
        return ResponseData.success().setData(menuDetailVos);
    }

}
